package com.xrit.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.constant.MessageConstant;
import com.xrit.exception.CustomException;
import com.xrit.pojo.CheckItem;
import com.xrit.query.QueryPageBean;
import com.xrit.result.PageResult;
import com.xrit.result.Result;
import com.xrit.service.CheckItemService;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("checkItem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    //添加
    @RequestMapping("/deleteCheckItem")
    @PreAuthorize("hasAuthority('aaa')")
    public Result deleteCheckItem(Integer id) throws CustomException {
        int row = checkItemService.delete(id);
        if (row>0){
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }else{
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    //添加
    @RequestMapping("/updateCheckItem")
    public Result updateCheckItem(@RequestBody CheckItem checkItem){
        int row = checkItemService.update(checkItem);
        if (row>0){
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }else{
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    //添加
    @RequestMapping("/addCheckItem")
    public Result addCheckItem(@RequestBody CheckItem checkItem){
        int row = checkItemService.add(checkItem);
        if (row>0){
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        }else{
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }


    //检查项列表支持分页+条件
    @RequestMapping("/findByPage")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult page = checkItemService.findByPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,page);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<CheckItem> list = checkItemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }
}
